<?php
class Article_model_article extends Model{
    private $table_article = 'article';
    
    public function get_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        return $this->db->select_row(
            $this->table_article,
            array('content'),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
    }
    
    
    public function edit_content($node_id, $data) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        $db = $this->db;
        $table = $this->table_article;
        
        $content = isset($data['content']) ? trim($data['content']) : '';
        
        $row = $db->select_row(
            $table,
            array('id'),
            array('node_id' => $node_id),
            array('node_id' => DB_DATA_INT)
        );
        $id = 0;
        
        if ($row) {
            $id = (int)$row['id']; 
            if ($id <= 0) tos_die();
            $db->update(
                $table,
                array('content' => $content),
                array('id' => $id),
                array(),
                array('id' => DB_DATA_INT)
            );
        } else {
            $id = $db->insert(
                $table,
                array('content' => $content, 'node_id' => $node_id),
                array('node_id' => DB_DATA_INT)
            );
        }
        return $id;
    }
    
    public function remvoe_content($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) tos_die();
        
        return $this->db->delete($this->table_article, array('node_id' => $node_id));
    }
}
?>